import Opt from '@/components/dt/info/Opt.js'
import toolMgr from '@/biz/base/tool.js'

let _this = null
class SearchOpt extends Opt {
  constructor(vm) {
    _this = vm
    super()
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    this.all = [
      this.organization(),
      {
        key: 'port',
        title: '端口筛选',
        select: {
          multiple: true,
          maxTagCount: 2,
          options: this.portList()
        },
        change: (val) => {
          if (val.value.length > 0) {
            if (val.value[val.value.length - 1] == '1') {
              _this.params.port = ['1']
            } else {
              _this.params.port = val.value.filter((item) => {
                return item != '1'
              })
            }
            _this.getTableData()
          } else {
            _this.$Message.error('请至少选择一个端口')
          }
        }
      },
      {
        key: 'fullAd',
        checkbox: {
          options: [
            {
              key: 'fullAd',
              value: '查看满额广告'
            }
          ]
        },
        change: () => {
          _this.getTableData()
        }
      }
    ]
  }

  portList() {
    return [
      {
        key: '1',
        value: '全部'
      },
      {
        key: 'COUNSELOR',
        value: '顾问端'
      },
      {
        key: 'HOUSES',
        value: '案场端'
      },
      {
        key: 'INTEGRATED',
        value: '综管端'
      },
      {
        key: 'FINANCE',
        value: '财务端'
      },
      {
        key: 'PERSONNEL',
        value: '人事端'
      },
      {
        key: 'OTHER',
        value: '其他'
      }
    ]
  }

  organization() {
    let orgs = []
    let opt = {
      key: 'orgIds',
      title: '组织筛选',
      tree: {
        options: orgs,
        saveParent: false,
        saveAll: false,
        maxTagCount: 2
      },
      change: () => {
        setTimeout(() => {
          _this.getTableData()
        }, 300)
      }
    }
    toolMgr.organization(2).then((res) => {
      if (res.length) {
        res.forEach((item) => {
          if (item.children) {
            item.children.unshift({
              label: item.label,
              orgName: item.orgName,
              title: item.title,
              orgId: item.orgId,
              value: item.value,
              id: item.id
            })
            item.id = item.id + 'a'
          }
        })
      }
      orgs = res
      opt.tree.options = orgs
    })
    return opt
  }
}

export default SearchOpt
